home *** CD-ROM | disk | FTP | other *** search
- Path: news.sunquest.com!kitk!kitk
- From: kitk@mudshark.sunquest.com (Kit Kauffmann)
- Newsgroups: comp.lang.c++
- Subject: Re: Bletcherous kludge!
- Date: Wed, 3 Apr 1996 08:17:54
- Organization: Sunquest
- Message-ID: <kitk.2158.00084CAA@mudshark.sunquest.com>
- References: <4jhr2b$9ou@portal.gmu.edu>
- NNTP-Posting-Host: kkauffma.sunquest.com
- X-Newsreader: Trumpet for Windows [Version 1.0 Rev A]
-
- In article <4jhr2b$9ou@portal.gmu.edu> rraffer1@osf1.gmu.edu (Ryan M Rafferty) writes:
-
- >I am trying to get the contents of a custom String class to be converted
- >into numeric data, ie float. The String class consists of :
-
- >Buffer: a null-terminated array of char
-
- >and several methods for common string stuff, including overloaded << and >>
- >operators. The code listed below is the only method I've been able to come up
- >with that works, but it is really an awful hack that outputs the contents of
- >the String instance to a file and then reads it back in to do the conversion.
-
- >I tried creating an instance of iostream, inputting to it from the String
- >instance, and then outputting from it into a float variable, but it just
- >didn't work. Any suggestions on how to do this the right way would be
- >greatly appreciated!
-
- >Ryan Rafferty
-
- >#include "String.h"
-
- >float String::Number() {
-
- > float i = 0;
-
- > // this conversion method is, in its current state, a HORRIBLE and
- > // AWFUL kludge to convert the contents of a String instance to a
- > // numeric type. It doesn't do any error checking, but what's worse,
- > // it USES A TEMPORARY FILE !!!! I'm so mad at myself! But I can't
- > // seem to be able to bend the will of the iostreams class to do what
- > // I want!
-
- > ofstream fout("./__temp");
- > fout << Buffer;
- > fout.close();
- > ifstream fin("./__temp");
- > fin >> i;
- > fin.close();
-
- > // YECH! A feeble attempt to hide my shame...
- > // execl("/bin/rm", "rm", "./__temp", (char *) 0);
-
- > return i;
- >};
-
- double String::Number()
- {
- return atof( Buffer );
- }
-
-
- Kit Kauffmann - kitk@mudshark.sunquest.com
- AKA 73363,447 (Compu$erve)
-
- Finger me for my public key
-